<template>
  <div class="body" v-loading="loading">
    <div>
      <el-button type="info" size="small" icon="el-icon-back" @click="gotoList">&nbsp;返&nbsp;回&nbsp;</el-button>
    </div>
    <el-form ref="form" :model="header" size="small" label-width="80px" :inline="true" class="header">
      <div class="audit_icon iconfont" v-if="header.status === 'audit'"></div>
      <el-row>
        <el-col :span="6">
          <el-form-item label="入库单号">{{ header.billNo }}</el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="入库日期">{{ header.createTime }}</el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="仓库名称">{{ getStockName(header.stockNo) }}</el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="入库箱数">{{ header.boxCount }}</el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="入库重量">{{ header.count ? Number(header.count.toFixed(6)): header.count }}</el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="库管">{{ header.createUserName }}</el-form-item>
        </el-col>
      </el-row>
      <br />
    </el-form>
    <el-table :data="dataList" class="detail" height="30vh">
      <el-table-column label="#" width="60" align="center" type="index"></el-table-column>
      <el-table-column label="仓位" width="160" prop="locationNo"></el-table-column>
      <el-table-column label="物料编码" width="160" prop="materialNo"></el-table-column>
      <el-table-column label="物料名称" width="230" prop="materialName"></el-table-column>
      <el-table-column label="物料规格" width="230" prop="materialSpec"></el-table-column>
      <el-table-column label="重量" width="100" align="right">
        <template #default="scope">
          {{ scope.row.count }}{{ scope.row.unit }}
        </template>
      </el-table-column>
      <el-table-column label="箱数" width="100" prop="boxCount" align="right"></el-table-column>
      <el-table-column label="批次号" min-width="160" prop="batchNo"></el-table-column>
      <el-table-column label="操作" width="160" fixed="right">
        <template #default="scope">
          <el-button size="medium" type="text" style="color: #409EFF" icon="el-icon-mouse" @click="getBox(scope.row._id)">查看箱号</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-table v-if="detailBoxList && detailBoxList.length > 0" :data="detailBoxList" class="detail" height="calc(70vh - 240px)">
      <el-table-column label="No." width="60" align="right" type="index"></el-table-column>
      <el-table-column label="箱号" width="160" prop="boxNo"></el-table-column>
      <el-table-column label="重量" width="100" align="right">
        <template #default="scope">
          {{ scope.row.count }}{{ scope.row.unit }}
        </template>
      </el-table-column>
      <el-table-column label="rfid" width="250" prop="rfid"></el-table-column>
    </el-table>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { getBoxNo } from '../../utils'

export default {
  data() {
    return {
      loading: false,
      materialLoading: false,
      materialList: [],
      supplierLoading: false,
      supplierList: [],
      header: {},
      dataList: [],
      detailBoxList: [],
      selection: [],
      stockList: [],
    }
  },
  async mounted() {
    await this.getStock()
    let billNo = this.$route.query.billNo
    let stockNo = this.$route.query.stockNo
    if (billNo && stockNo) {
      return this.getBill(billNo, stockNo)
    }
  },
  methods: {
    async getStock() {
      this.loading = true;
      try {
        let res = await this.$api.getChildDictionaryListByKeys({
          keys: ['stock']
        });
        if (!res.res) return;
        this.stockList = res.stock;
      } finally {
        this.loading = false;
      }
    },
    getStockName(val) {
      if (this.stockList && this.stockList.length > 0) {
        for(let i = 0; i < this.stockList.length; i++) {
          if (this.stockList[i].key === val) {
            return this.stockList[i].name;
          }
        }
      }
      return '';
    },
    gotoList() {
      return this.$router.push('/other/otherInList')
    },
    async getBill(billNo, stock) {
      this.loading = true
      try {
        let res = await this.callApi('get_otherin_bill_detail', {
          billNo: billNo,
          stockNo: stock
        })
        if (res.res) {
          this.header = res.header || {}
          this.dataList = res.data || []
        }
      } finally {
        this.loading = false
      }
    },
    async getBox(id) {
      this.loading = true
      try {
        let res = await this.callApi('get_otherin_bill_detail_box', {
          _detailId: id
        })
        if (res.res) {
          this.detailBoxList = res.data || []
          this.changeDetailCount()
        }
      } finally {
        this.loading = false
      }
    },
  }
}
</script>

<style lang="scss" scoped>
.body {
  padding: 20px 0;
  .header {
    margin-top: 20px;
    position: relative;
    .header_row {
      display: flex;
      >div {
        flex-shrink: 0;
      }
      :deep(.produceDate) {
        .el-input {
          width: 100%!important;
        }
      }
    }
    .audit_icon {
      position: absolute;
      top: -10px;
      left: 60px;
      font-size: 48px;
      color: red;
    }
    .material {
      width: 100%;
    }
    .p3 {
      :deep(.el-form-item__content) {
        display: flex;
        .comment {
          margin-left: 10px;
          word-break: keep-all;
        }
      }
    }
  }
  .detail_header {
    display: flex;
    justify-content: space-between;
    margin-top: 10px;
    .stat {
      display: flex;
      font-size: 15px;
      >div {
        display: flex;
        align-items: baseline;
        line-height: 32px;
        .label {
          text-align: right;
          margin-right: 5px;
          color: #aaa;
        }
        .value {
          min-width: 50px;
          margin-right: 10px;
          color: #666;
        }
      }
    }
  }
}
</style>
